# -*- coding: UTF-8 -*-
"""
@Filename:exerc2.py
@author:Cora
@time:2025-05-09
"""
import requests


def translate(word):
    # 百度翻译API的URL
    url = "https://fanyi.baidu.com/sug"

    # 模拟浏览器请求的headers
    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.3"
    }

    # 发送POST请求
    data = {
        "kw": word
    }

    response = requests.post(url, headers=headers, data=data)

    # 检查响应状态
    if response.status_code == 200:
        result = response.json()
        if result.get("data"):
            # 获取翻译结果
            translation = result["data"][0]["v"]
            return translation
        else:
            return "No translation found."
    else:
        return "Error: Unable to reach the translation service."


if __name__ == "__main__":
    # 用户输入单词
    word = input("请输入需要翻译的单词: ")
    # 获取翻译结果
    translation = translate(word)
    # 输出翻译结果
    print(f"翻译结果: {translation}")